﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Negro
{
    public partial class Salida : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (IsPostBack)
            {
                if (Session["SALIDA_FORM"] != null)
                {
                    Salida salida = (Salida)Session["SALIDA_FORM"];

                    this.lista.DataSource = salida.lista.DataSource;

                }
            }
          

        }

        protected void btnAgregar_Click(object sender, EventArgs e)
        {

            SalidaBusiness business = new SalidaBusiness();

            productos bean = business.consulta(this.claveProcucto.Text);

            if (bean != null)
            {

                DataTable dataTable = (DataTable) this.lista.DataSource;

                List<productos> lista = new List<productos>();

                if (dataTable != null)
                {
                    List<productos> listaTotal = Util.Convert.ToCollection<productos>(dataTable);
              
                    foreach (productos item in listaTotal)
                    {
                        productos aux = item;

                        lista.Add(aux);
                    }
                
                }
                
                lista.Add(bean);

                dataTable = Util.Convert.ToDataTable<productos>(lista.ToList());

                this.lista.DataSource = dataTable;

                this.lista.DataBind();

                Session["SALIDA_FORM"] = this;
            }
        }

        protected void btnGuardar_Click(object sender, EventArgs e)
        {
            if (Session["SALIDA_FORM"] != null)
            {
                Salida salida = (Salida)Session["SALIDA_FORM"];

                this.lista = salida.lista;

                DataTable dataTable = (DataTable)this.lista.DataSource;

                List<productos> lista = new List<productos>();

                if (dataTable != null) 
                {
                    List<productos> listaTotal = Util.Convert.ToCollection<productos>(dataTable);

                    SalidaBusiness business = new SalidaBusiness();

                    business.guardar(listaTotal);
                
                }

            }
        }

        protected void btnRegresar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/aspx/Menu.aspx");
        }
    }
}